package com.cloud.tsp.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloud.api.annotation.TransUser;
import com.cloud.api.domain.tsp.*;
import com.cloud.api.myenum.CommandStatusEnum;
import com.cloud.api.myenum.ExecuteStatus;
import com.cloud.core.exception.MyException;
import com.cloud.mybatis.domain.TableDataInfo;
import com.cloud.tsp.mapper.CommandExecHistoryMapper;
import com.cloud.tsp.service.ICommandExecHistoryService;
import com.cloud.tsp.service.ICommandExecLatestService;
import com.cloud.tsp.service.IVehicleDataService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import jakarta.annotation.Resource;
import java.util.List;
import java.util.Optional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author haohaibo
 * @since 2023-11-30
 */
@Service("commandExecHistoryService")
public class CommandExecHistoryServiceImpl extends ServiceImpl<CommandExecHistoryMapper, CommandExecHistory> implements ICommandExecHistoryService {

    @Resource
    private ICommandExecLatestService commandExecLatestService;
    @Resource
    private CommandExecHistoryMapper commandExecHistoryMapper;
    @Resource
    private IVehicleDataService vehicleDataService;
    @Resource(name = "myRedis")
    private RedisTemplate<String, Object> redisTemplate;

    /*@Override
    @TransUser
    public TableDataInfo<CommandExecHistory> selectExecHistory(String id) {
        CommandExecLatest execLatest = commandExecLatestService.getById(id);

        LambdaQueryWrapper<CommandExecHistory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CommandExecHistory::getVin,execLatest.getVin())
                .eq(CommandExecHistory::getCommand,execLatest.getCommand());
        List<CommandExecHistory> data = commandExecHistoryMapper.selectList(queryWrapper);
        if (data.size()>0){
            data.forEach(a -> a.setStatus(CommandStatusEnum.getInstance(a.getStatus())));
        }
        return TableDataInfo.build(data);
    }*/

    @Override
    public List<Command> getCommandCount(String type) {
        return commandExecHistoryMapper.getCommandCount(type);
    }

    @Override
    public TableDataInfo<CommandExecHistory> selectIssueHistory(QueryIssueHistory para) {
        IPage<CommandExecHistory> page = new Page<>(para.getPage(),para.getPageSize());
        IPage<CommandExecHistory> data = commandExecHistoryMapper.selectIssueHistory(page, para);
        desensitize(data.getRecords());
        return TableDataInfo.build(data);
    }

    @Override
    public CommandExecHistory selectPendingCommand(String vin,String commandId) {
        LambdaQueryWrapper<CommandExecHistory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CommandExecHistory::getVin,vin)
                .eq(CommandExecHistory::getCommand,commandId)
                .eq(CommandExecHistory::getStatus, ExecuteStatus.PENDING);
        return commandExecHistoryMapper.selectOne(queryWrapper,false);
    }

    @Override
    public CommandDetail selectCommandExecDetail(String id) {
        CommandExecHistory execHistory = commandExecHistoryMapper.selectIssueHistoryById(id);
        Optional.ofNullable(execHistory).orElseThrow(()->new MyException("wds.exception.id"));
        CommandDetail commandDetail = new CommandDetail();
        LockStatus lockStatus = vehicleDataService.selectVehicleData(execHistory.getVin());
        if (lockStatus != null){
            commandDetail.setLockStatus(lockStatus.getLockStatus());
            commandDetail.setVehicleStatus(lockStatus.getOnlineStatus());
        }
        commandDetail.setCommandStatus(execHistory.getStatus());
        commandDetail.setCommandSuccessTime(execHistory.getUpdateTime());
        if (execHistory.getStatus() == ExecuteStatus.PENDING){
            String msg = execHistory.getVin()+"-"+execHistory.getCommandCode()+"-"+execHistory.getDid();
            Boolean member = redisTemplate.opsForSet().isMember("COMMAND2BOX", msg);
            if (Boolean.FALSE.equals(member)){
                //对比实时数据和指令
                //存库,给结果并返回
            }
        }
        return commandDetail;
    }

    public void desensitize(List<CommandExecHistory> list){
        for (CommandExecHistory commandExecHistory : list) {
            commandExecHistory.setVin(StrUtil.hide(commandExecHistory.getVin(), 8, 11));
        }
    }
}